import {createWebHistory, createRouter} from "vue-router";
import App from '.././App.vue'
import UserList from '../views/UserList.vue'
import MyLogin from '../views/MyLogin.vue'
import NotFound from '../views/NotFound.vue'
import UserRegister from '../views/UserRegister.vue'
import HomePage from '../views/HomePage.vue'
import index from '../views/index.vue'
import child1 from '../views/child1.vue'
import ProjectList from '../views/ProjectList.vue'
import MainPage from '../views/MainPage.vue'
import UrlList from '../views/UrlList.vue'
import GlobalVariable from '../views/GlobalVariable.vue'
import Modules from '../views/Modules.vue'

const routes = [
    {
        path: "/",
        redirect: '/MyLogin'
    },
    {
        path: "/",
        name: "App",
        component: App,
        children: [
            // {
            //     path: "MyLogin",
            //     name: "MyLogin",
            //     component: MyLogin,
            // },
            {
                path: "UserList",
                name: "UserList",
                component: UserList,
            },
        ]
    },
    {
        path: "/UserRegister",
        name: "UserRegister",
        component: UserRegister,
        children: []
    },
    {
        path: "/MyLogin",
        name: "MyLogin",
        component: MyLogin,
        children: []
    },
    {
        path: "/MainPage",
        name: "MainPage",
        component: MainPage,
        children: [
            {
                path: "/projectlist",
                name: "ProjectList",
                component: ProjectList,
                children: []
            },
            {
                path: "/child1",
                name: "child1",
                component: child1,
                children: []
            },
            {
                path: "/HomePage",
                name: "HomePage",
                component: HomePage,
                children: []
            },
            {
                path: "/UrlList",
                name: "UrlList",
                component: UrlList,
                children: []
            },
            {
                path: "/GlobalVariable",
                name: "GlobalVariable",
                component: GlobalVariable,
                children: []
            },
            {
                path: "/Modules",
                name: "Modules",
                component: Modules,
                children: []
            },
        ]
    },
    {
        path: "/index",
        name: "index",
        component: index,
        children: []
    },

    {

        name: '404',
        path: '/404',
        component: NotFound
    },
    {
        path: '/:catchAll(.*)', // 此处需特别注意至于最底部
        redirect: '/404'
    }

];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const tokenstr = window.localStorage.getItem('token')
    console.log(to)
    console.log(from)
    if (from.path == '/MyLogin' && !tokenstr && to.path == '/UserRegister') {
        return next()
    }
    if (to.path == '/MyLogin') {
        // if(to.path == '/MyLogin') {
        if (tokenstr) {
            return next('/HomePage')
        }
        return next();
    }
    // console.log(tokenstr)
    if (!tokenstr) return next('/MyLogin');
    next();
})


export default router;